<?php
namespace app\crm\controller;
use think\Loader;
use think\Cache;

class Ordermanager extends CrmController
{
    /**
     * 订单列表界面
     */
    public function orderList()
    {	
		//分配增删改的权限
		$menuname = "Order List";
		$p_menuname = $_SESSION['menu'][$menuname]; //父菜单
		$priv = $_SESSION["user_priv"][$p_menuname][$menuname];	
		$this->assign("username",$_SESSION['user_info']['username']);
        $this->assign("priv",$priv);
		$this->assign("customer_code",empty($_REQUEST["customer_code"])?0:$_REQUEST["customer_code"]);
		
		$Logistics = Loader::model('Ordermanager')->getLogisticsMode();
        $this->assign("Logistics",$Logistics);
		
		$arrUsers = Loader::model('Users')->getUsersList();
        $this->assign("arrUsers",$arrUsers);
		
        $result = Loader::model('Department')->recursionDepartment();
        $result = Loader::model('Department')->recursionDepartmentArr($result);
        $this->assign('options', $result);
		// 模板输出
        return $this->fetch('orderList');
    }

    /**
     * 获取订单列表
     */
	public function orderDataList()
	{
		$page = $_REQUEST["page"];
        $limit = $_REQUEST["limit"];
		
		//需先判断传值是否为空
        $where = "(1 AND (modify_price = 'N' OR (modify_price = 'Y' AND audit_status = 'Y') )";
        $where .= empty($_REQUEST["name"])?"":" AND consignee like '%".$_REQUEST["name"]."%'";
        $where .= empty($_REQUEST["create_user"])?"":" AND createname = '".$_REQUEST["create_user"]."'";
        $where .= empty($_REQUEST["phone"])?"":" AND phone = '".$_REQUEST["phone"]."'";
		$where .= empty($_REQUEST["start_createtime"])?"":" AND createtime >= '".$_REQUEST["start_createtime"]."'";
		$where .= empty($_REQUEST["dept_id"])?"":" AND dept_id = '".$_REQUEST["dept_id"]."'";
		$where .= empty($_REQUEST["import_mark"])?"":" AND import_mark = '".$_REQUEST["import_mark"]."'";
		$where .= empty($_REQUEST["end_createtime"])?"":" AND createtime <= '".$_REQUEST["end_createtime"]."'";
        $where .= empty($_REQUEST["customer_code"])?"":" AND customer_code = '".$_REQUEST["customer_code"]."'";
		$where .= empty($_REQUEST["order_status"])?"":" AND order_status = '".$_REQUEST["order_status"]."'";
		$where .= empty($_REQUEST["pay_status"])?"":" AND pay_status = '".$_REQUEST["pay_status"]."'";
		$where .= empty($_REQUEST["shipping_status"])?"":" AND shipping_status = '".$_REQUEST["shipping_status"]."'";
		$where .= empty($_REQUEST["logistics_account"])?"":" AND logistics_account = '".$_REQUEST["logistics_account"]."'";
		$where .= empty($_REQUEST["logistics_state"])?"":" AND logistics_state = '".$_REQUEST["logistics_state"]."'";
		$where .= empty($_REQUEST["shopping_name"])?"":" AND shopping_name = '".$_REQUEST["shopping_name"]."'";
		$where .= empty($_REQUEST["order_num"])?"":" AND order_num = '".$_REQUEST["order_num"]."'";
        $where .= " )";
		
		
        $arrData = Loader::model('Ordermanager')->getOrderData($page,$limit,$where);
        echo json_encode($arrData);
	}
	
    /**
     * 商品列表界面
     */
	public function goodsList()
    {	
        // 模板输出
        return $this->fetch('goodsList');
    }
	
	/**
     * 方案列表界面
     */
	public function saleProgramList()
    {
        $typeData = Loader::model('Saleprogram')->getProgramType();
        $this->assign("typeData",$typeData);
        $this->assign("task_id",$_REQUEST['task_id']);
        // 模板输出
        return $this->fetch('saleProgramList');
    }
	
    /**
     * 添加订单界面
     */
	public function addOrder()
    {	
		//分配增删改的权限
		$menuname = "Order List";
		$p_menuname = $_SESSION['menu'][$menuname]; //父菜单
		$priv = $_SESSION["user_priv"][$p_menuname][$menuname];	
		$username = $_SESSION['user_info']['username'];
		$this->assign("username",$username);
        $this->assign("priv",$priv);
		$task_id = $_REQUEST["task_id"];
		$this->assign("task_id",$task_id);
		$times = date("Y-m-d H:i:s");
        $this->assign("times",$times);
        
		if(!empty($_REQUEST["customer_code"])){
			$customerData = Loader::model('Customer')->getAddOrder($_REQUEST["customer_code"]);
            if (empty($customerData)) {
                $customerData = Loader::model('Callplatform')->getAddOrder($_REQUEST["customer_code"], $_REQUEST['task_id']);
            }
			//print_r($customerData);die;
			//判断号码是否加密
			if($priv["encrypt_phone"] == "Y"){
				if($username != "admin"){
					$customerData["phone"] = substr($customerData["phone"],0,3)."*****".substr($customerData["phone"],-2);
					$customerData["telephone"] = substr($customerData["telephone"],0,3)."*****".substr($customerData["telephone"],-2);
				}
			}
			$this->assign("customerData",$customerData);
		}
        // 模板输出
        return $this->fetch('addOrder');
    }
	
    /**
     * 添加订单
     */
	public function insertOrder()
    {	
		Loader::model('Ordermanager')->insertOrder($_REQUEST);
	}
	
    /**
     * 更新订单状态
     */
	public function updateStatus(){
		Loader::model('Ordermanager')->updateStatus();
	}
	
    /**
     * 编辑订单界面
     */
	public function editOrder()
    {	
		$username = $_SESSION["user_info"]["username"];
		//分配增删改的权限
		$menuname = "Order List";
		$p_menuname = $_SESSION['menu'][$menuname]; //父菜单
		$priv = $_SESSION["user_priv"][$p_menuname][$menuname];		
		$this->assign("username",$_SESSION['user_info']['username']);
		$this->assign("priv",$priv);
		
		$order_num = $_GET["order_num"];
		$arrData = Loader::model('Ordermanager')->getOrderDetail($order_num);
		//判断号码是否加密
		if($priv["encrypt_phone"] == "Y"){
			if($username != "admin"){
				$arrData["phone"] = substr($arrData["phone"],0,3)."*****".substr($arrData["phone"],-2);
				$arrData["telephone"] = substr($arrData["telephone"],0,3)."*****".substr($arrData["telephone"],-2);
			}
		}
        $this->assign("arrData",$arrData);
		$Logistics = Loader::model('Ordermanager')->getLogisticsMode();
        $this->assign("Logistics",$Logistics);
        // 模板输出
        return $this->fetch('editOrder');
    }
	
    /**
     * 删除订单
     */
	public function delOrder()
	{
		Loader::model('Ordermanager')->delOrder();
	}
	
   /**
    * 编辑页面商品信息
    */
	public function orderGoods()
	{
		$arrData = Loader::model('Ordermanager')->getOrderGoods($_REQUEST["id"]);
        echo json_encode($arrData);
	}
	
	/**
	 * 加载checkPriceList模板
	 */
	public function checkPriceList()
	{
		//分配增删改的权限
		$menuname = "Check Price";
		$p_menuname = $_SESSION['menu'][$menuname]; //父菜单
		$priv = $_SESSION["user_priv"][$p_menuname][$menuname];	
		$this->assign("username",$_SESSION['user_info']['username']);
        $this->assign("priv",$priv);
		
		$arrUsers = Loader::model('Users')->getUsersList();
        $this->assign("arrUsers",$arrUsers);
		
		$result = Loader::model('Department')->recursionDepartment();
        $result = Loader::model('Department')->recursionDepartmentArr($result);
        $this->assign('options', $result);
		return $this->fetch('checkPriceList');
	}
	
	//获取价格审核列表
	public function checkPriceDataList()
	{
		$arrData = Loader::model('Ordermanager')->checkPriceDataList();
        echo json_encode($arrData);
	}
	
	//加载editCheckPrice模板
	public function editCheckPrice()
	{
		$id = $_GET["id"];
		$arrData = Loader::model('Ordermanager')->getOrderDetail($id);
        $this->assign("arrData",$arrData);
		return $this->fetch('editCheckPrice');
	}
	
	//审核价格
	public function checkPrice()
	{
		Loader::model('Ordermanager')->checkPrice();
	}
	
	//批量修改物流信息
	public function updateLogistics()
	{
		Loader::model('Ordermanager')->updateLogistics();
	}
	
	//打印货单
	public function printInvoiceList()
	{
		header("Content-Type:text/html; charset=utf-8");
		$arrData = Loader::model('Ordermanager')->printInvoiceList();
		$max = count($arrData);
		$this->assign("arrData",$arrData);
		$this->assign("max",$max);
		return $this->fetch('printInvoiceList');
	}
	
	//打印顺丰快递单
	public function printSFList()
	{
		header("Content-Type:text/html; charset=utf-8");
		$arrData = Loader::model('Ordermanager')->printSFList();
		$para_sys = Cache::get('system');
		
		$this->assign("arrData",$arrData);
		$this->assign("para_sys",$para_sys);
		return $this->fetch('printSFList');
	}
	
	//获取客户地址
	public function synchroAddress()
	{
		$customer_code = $_REQUEST["customer_code"];
		$arrData = Loader::model('Customer')->getAddress($customer_code);
		echo json_encode($arrData);
	}
}

?>